var gnid;
var qqcs;

$(function () {
    $(window).resize(function(){
        setTimeout(function () {
            fitCoulms();
        },300);
    });
    gnid = $('#gnid').val();
    require.config({
        paths: {
            echarts: echarts2_dist_url
        }
    });
    //初始化星星
    initRatingStarts();
    //初始化历史评价图
    initLspj();
    //初始化表格
    initDataGrid();
});

//初始化星星
function initRatingStarts() {
    $("#wdpf").rating({
        theme: 'krajee-fa',
        language: 'zh',
        min: 0,
        max: 5,
        step: 1,
        size: 'sm',
        showClear: true
    });
}

//初始化评价列表并查询
function initDataGrid() {
    var fields = [[{
        title: '评论人',
        field: 'xm',
        sortable: false,
        width: 200, formatter: function (value) {
            if (null == value) {
                value = "";
            }
            return "<span title='" + value + "'>" + value + "</span>";
        }
    }, {
        title: '评论等级',
        field: 'dj',
        sortable: false,
        width: 200, formatter: function (value) {
            if (null == value) {
                value = "";
            }
            return "<span title='" + value + "'>" + value + "</span>";
        }
    }, {
        title: '评价内容',
        field: 'nr',
        sortable: false,
        width: 300,
        formatter: function (value) {
            if (null == value) {
                value = "";
            }
            return "<span title='" + value + "'>" + value + "</span>";
        }
    }, {
        title: '评论时间',
        field: 'sj',
        sortable: false,
        width: 200, formatter: function (value) {
            if (null == value) {
                value = "";
            }
            return "<span title='" + value + "'>" + value + "</span>";
        }
    }]];

    $('#tt').datagrid({
        collapsible: false,
        rownumbers: true,
        remoteSort: false,
        nowrap: true,
        striped: true,
        fitColumns: true,
        singleSelect: true,
        fit: false,
        pagination: true,
        pageNumber: 1,
        pageSize: 5,
        pageList: [5],
        columns: fields
    });
    //请求参数
    qqcs = {
        "gnid": gnid
    };
    //查询开始
    $('#tt').datagrid('clearSelections');
    $('#tt').datagrid({url: "/portal/user/evaluate/queryPjLb.do", queryParams: qqcs});
}

//初始化评分图表
function initLspj() {
    qqcs = {"gnid": gnid};
    $.ajax({
        url: "/portal/user/evaluate/queryLsPf.do",
        data: qqcs,
        type: "post",
        timeout: sys_timeout,
        dataType: "json",
        async: false,
        error: function (XMLHttpRequest, textStatus) {
            closeProgress();
            innerToast.warn("抱歉，系统暂时无法访问,请稍后再试");
            console.log(textStatus);
        },
        success: function (respText) {
            closeProgress();
            if (checkResponse(respText)) {
                var djList = respText.repData.djList;
                var sjList = respText.repData.sjList;
                var djArr = new Array();
                var sjArr = new Array();
                for (var i = 0; i < djList.length; i++) {
                    djArr[i] = djList[i].djmc;
                }
                for (var i = 0; i < sjList.length; i++) {
                    sjArr[i] = sjList[i].gs;
                }
                require(
                    [
                        'echarts',
                        'echarts/chart/bar' // 使用柱状图就加载bar模块，按需加载
                    ],
                    function (ec) {
                        var myChart = ec.init(document.getElementById('lspj_div'));
                        option = {
                            tooltip: {
                                trigger: 'axis'
                            },
                            xAxis: [{
                                type: 'value',
                                splitLine: false,
                                boundaryGap: false,
                                splitLine: {
                                    show: false
                                },
                            }],
                            yAxis: [{
                                type: 'category',
                                data: djArr
                            }],
                            series: [{
                                name: '评价次数',
                                type: 'bar',
                                data: sjArr,
                                labelLine: {
                                    normal: {
                                        show: false
                                    }
                                }
                            }]
                        };
                        myChart.setOption(option);
                    }
                );
            }
            else {
                innerToast.warn(respText.msg);
            }
        }
    });

}

function savePj() {
    //保存评价
    var wdpf = $('#wdpf').val();
    var wdpj = $('#wdpj').val();
    if ("" == wdpf) {
        innerToast.warn("请选择评分！");
        return;
    } else if ("" == wdpj) {
        innerToast.warn("请填写评价！");
        return;
    }
    openProgress();
    qqcs = {"gnid": gnid, "wdpf": wdpf, "wdpj": wdpj};
    $.ajax({
        url: "/portal/user/evaluate/savePj.do",
        data: qqcs,
        type: "post",
        timeout: sys_timeout,
        dataType: "json",
        async: false,
        error: function (XMLHttpRequest, textStatus) {
            closeProgress();
            innerToast.warn("抱歉，系统暂时无法访问,请稍后再试");
            console.log(textStatus);
        },
        success: function (respText) {
            closeProgress();
            if (checkResponse(respText)) {
                innerToast.info("评论成功！");
                //初始化历史评价图
                initLspj();
                //刷新评价列表
                qqcs = {
                    "gnid": gnid
                };
                //查询开始
                $('#tt').datagrid('clearSelections');
                $('#tt').datagrid({url: "/portal/user/evaluate/queryPjLb.do", queryParams: qqcs});
            }
            else {
                innerToast.warn(respText.msg);
            }
        }
    });
}


function openProgress() {
    $.messager.progress({
        title: '提示',
        msg: '请稍候……',
        text: ''
    });
}

function closeProgress() {
    $.messager.progress('close');
}